﻿using System;
using System.Collections.Generic;
using System.Text;
using TCCHWLib.Commands;
using System.Windows.Forms;

namespace TCCCompiler
{
    class SimpleCondition:IExpression
    {
        List<IExpression> Parts = new List<IExpression>();
        TreeNode Currennt_Node;
        List<string> operators = new List<string>();
        bool t_for=false;
        bool is_arr=false;
        string t_for_index;

         private void Get_Condition()
        {
            for (int i = 0; i < Currennt_Node.Nodes.Count; i++)
            {
                switch (Currennt_Node.Nodes[i].Text)
                {
                    //case "EXP":
                    //    SimpleExpression exp = new SimpleExpression(Currennt_Node.Nodes[i],Current_Batch);
                    //    //exp.Get_Expression();
                    //    Current_Batch = exp.Current_Batch;
                    //    Parts.Add(exp);
                    //    break;

                    //case "Condition":
                    //    SimpleCondition condition  = new SimpleCondition(Currennt_Node.Nodes[i],Current_Batch);
                    //   // condition.Get_Condition();
                    //    Current_Batch = condition.Current_Batch;
                    //    Parts.Add(condition);
                    //    break;
                    case "ConditionTerm":
                        Term term = new Term(Currennt_Node.Nodes[i], Current_Batch,t_for,t_for_index,is_arr);
                        Current_Batch = term.Current_Batch;
                        Parts.Add(term);
                        break;


                    case "ConditionOperator":
                        operators.Add(Currennt_Node.Nodes[i].Nodes[0].Text.Trim());
                        break;
                }
            }
        }
       public void evaluate_Condition()
       {
           ParameterType parameter1_type;
           ParameterType parameter2_type;
           //this.Commands.AddRange(Parts[0].Commands);
           object parameter_1 = Parts[0].Value;
           parameter1_type = Parts[0].Type;
           Parts.RemoveAt(0);
           object parameter_2;

           for (int i = 0; i < Parts.Count; i++)
           {
               parameter_2 = Parts[i].Value;
               parameter2_type = Parts[i].Type;
               switch (operators[i].Trim())
               {
                   //case "==":
                   //    EqualCommand equal = new EqualCommand(parameter_1, parameter1_type, parameter_2, parameter2_type);
                   //    Current_Batch.AddCommand(equal);

                   //    parameter_1 = Current_Batch.CommandList.Count - 1;
                   //    parameter1_type = ParameterType.Instruction;
                   //    break;
                       
                   //case "<=":
                   //    LessThanOrEqualCommand lessThanOrEqual = new LessThanOrEqualCommand(parameter_1, parameter1_type, parameter_2, parameter2_type);
                   //   Current_Batch.AddCommand(lessThanOrEqual);
                   //   parameter_1 = Current_Batch.CommandList.Count - 1;
                   //   parameter1_type = ParameterType.Instruction;
                   //    break;

                   //case "<":
                   //    LessThanCommand lessThan = new LessThanCommand(parameter_1, parameter1_type, parameter_2, parameter2_type);
                   //   Current_Batch.AddCommand(lessThan);

                   //   parameter_1 = Current_Batch.CommandList.Count - 1;
                   //   parameter1_type = ParameterType.Instruction;
                   //    break;
                   //case ">=":
                       
                   //    GreaterThanOrEqualCommand greaterThanOrEqual = new GreaterThanOrEqualCommand(parameter_1, parameter1_type, parameter_2, parameter2_type);
                   //    Current_Batch.AddCommand(greaterThanOrEqual);
                       
                   //    parameter_1 = Current_Batch.CommandList.Count-1;
                   //    parameter1_type = ParameterType.Instruction;
                   //    break;
                   //case ">":
                   //    GreaterThanCommand greaterThan = new GreaterThanCommand(parameter_1, parameter1_type, parameter_2, parameter2_type);
                   //   Current_Batch.AddCommand(greaterThan);

                   //   parameter_1 = Current_Batch.CommandList.Count - 1;
                   //   parameter1_type = ParameterType.Instruction;
                   //    break;
                   //case "!=":
                   //    NotEqualCommand notEqual = new NotEqualCommand(parameter_1, parameter1_type, parameter_2, parameter2_type);
                   //    Current_Batch.AddCommand(notEqual);

                   //    parameter_1 = Current_Batch.CommandList.Count - 1;
                   //    parameter1_type = ParameterType.Instruction;
                   //    break;
                   case "&&":
                       AndCommand And = new AndCommand(parameter_1, parameter1_type, parameter_2, parameter2_type);
                       Current_Batch.AddCommand(And);

                       parameter_1 = Current_Batch.CommandList.Count - 1;
                       parameter1_type = ParameterType.Instruction;
                       break;

                   case "||":
                       OrCommand Or = new OrCommand(parameter_1, parameter1_type, parameter_2, parameter2_type);
                       Current_Batch.AddCommand(Or);

                       parameter_1 =Current_Batch.CommandList.Count - 1;
                       parameter1_type = ParameterType.Instruction;
                       break;

               }
           }
           this.Value = Current_Batch.CommandList.Count - 1;
           this.Type = ParameterType.Instruction;
       }
       public SimpleCondition(TreeNode Node,BatchCommand batch,bool T_for,string T_for_index,bool Is_arr)
        {
            Current_Batch = batch;
            Currennt_Node = Node;
            t_for = T_for;
            t_for_index = T_for_index;
            is_arr = Is_arr;
            Get_Condition();
            evaluate_Condition();
            
        }
    }
}
